package cn.cs.controller;

import cn.cs.common.Dto;
import cn.cs.common.DtoUtil;
import cn.cs.service.ShoppingCarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/shoppingCar")
public class ShoppingCarController {
    @Autowired
    private ShoppingCarService shoppingCarService;

    /****
     * 添加购物车
     * @param request
     * @param
     * @return
     */
    @RequestMapping("/addCar")
    @ResponseBody
    public Dto addCar(HttpServletRequest request, HttpServletResponse response){
        Dto dto=null;
        try {
            response.setHeader("Access-Control-Allow-Origin", "*");
            dto=shoppingCarService.addCar(request);
        }catch (Exception e){
            e.printStackTrace();
            return DtoUtil.returnFail("添加失败","0002");
        }
        return dto;
    }

    /****
     * 我的购物车
     * @param request
     * @param
     * @return
     */
    @RequestMapping("/myShopCar")
    @ResponseBody
    public Dto myShopCar(HttpServletRequest request, HttpServletResponse response){
        Dto dto=null;
        try {
            response.setHeader("Access-Control-Allow-Origin", "*");
            dto=shoppingCarService.myShopCar(request);
        }catch (Exception e){
            e.printStackTrace();
            return DtoUtil.returnFail("查询失败","0002");
        }
        return dto;
    }


    /****
     * 结算购物车
     * @param request
     * @param
     * @return
     */
    @RequestMapping("/seetShopCar")
    @ResponseBody
    public Dto seetShopCar(HttpServletRequest request, HttpServletResponse response){
        Dto dto=null;
        try {
            response.setHeader("Access-Control-Allow-Origin", "*");
            dto=shoppingCarService.seetShopCar(request);
        }catch (Exception e){
            e.printStackTrace();
            return DtoUtil.returnFail("查询失败","0002");
        }
        return dto;
    }

    /****
     * 更新购物车中商品状态
     * @param request
     * @param
     * @return
     */
    @RequestMapping("/updateProState")
    @ResponseBody
    public Dto updateProState(HttpServletRequest request, HttpServletResponse response){
        Dto dto=null;
        try {
            response.setHeader("Access-Control-Allow-Origin", "*");
            dto=shoppingCarService.updateProState(request);
        }catch (Exception e){
            e.printStackTrace();
            return DtoUtil.returnFail("添加失败","0002");
        }
        return dto;
    }
}
